package com.bingxue.edu.common.enums;

import cn.hutool.core.util.StrUtil;
import com.bingxue.edu.framework.enums.Dict;
import com.bingxue.edu.framework.enums.DictEnum;
import com.mybatisflex.annotation.EnumValue;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * 文件类型
 * @author panbo
 * @since 2024/7/19
 */
@Getter
@RequiredArgsConstructor
@Dict("文件类型")
public enum FileTypeEnum implements DictEnum<String> {

    UNKNOWN("UNKNOWN", "其它", Collections.emptyList()),
    IMAGE("IMAGE", "图片", List.of("jpg", "jpeg", "png", "gif", "bmp", "webp", "ico", "psd", "tiff", "dwg", "jxr", "apng", "xcf")),
    DOC("DOC", "文档", List.of("txt", "pdf", "doc", "xls", "ppt", "docx", "xlsx", "pptx")),
    VIDEO("VIDEO", "视频", List.of("mp4", "avi", "mkv", "flv", "webm", "wmv", "m4v", "mov", "mpg", "rmvb", "3gp")),
    AUDIO("AUDIO", "音频", List.of("mp3", "flac", "wav", "ogg", "midi", "m4a", "aac", "amr", "ac3", "aiff")),
    ;

    @EnumValue
    private final String value;
    private final String label;
    private final List<String> extensions;

    /**
     * 根据扩展名查询
     *
     * @param extension 扩展名
     * @return 文件类型
     */
    public static FileTypeEnum getByExtension(String extension) {
        return Arrays.stream(FileTypeEnum.values())
                .filter(t -> t.getExtensions().contains(StrUtil.emptyIfNull(extension).toLowerCase()))
                .findFirst()
                .orElse(FileTypeEnum.UNKNOWN);
    }
}

